const getheight = function () {
  wx.getSystemInfo({
    success: function (res) {
      wx.setStorageSync('statusBarHeight', res.statusBarHeight)
    }
  })
}
// 判断数组
const isArray = Array.isArray || function (obj) {
  return obj instanceof Array;
}
//判断字符是否为空的方法
const isEmpty = function (obj) {
  if (typeof obj == "undefined" || obj == null || obj == "") {
    return true;
  } else {
    return false;
  }
}
// 获取字符串长度
const getStrlen = function (str = '') {
  let bytesCount = 0
  for (var i = 0; i < str.length; i++) {
    var c = str.charAt(i);
    //匹配双字节
    if (/^[\u0000-\u00ff]$/.test(c)) {
      bytesCount += 1;
    } else {
      bytesCount += 2;
    }
  }
  return bytesCount
}

const cutString = function (str, len, suffix) {
  if (!str) return "";
  if (len <= 0) return "";
  if (!suffix) suffix = "";
  var templen = 0;
  for (var i = 0; i < str.length; i++) {
    var c = str.charAt(i);
    //匹配双字节
    if (/^[\u0000-\u00ff]$/.test(c)) {
      templen += 1;
    } else {
      templen += 2;
    }
    if (templen == len) {
      return str.substring(0, i + 1) + suffix;
    } else if (templen > len) {
      return str.substring(0, i) + suffix;
    }
  }
  return str;
}
const strFormat = function (str) {
  let arr
  let arrItems = ''
  if (isEmpty(str)) return ''
  arr = str.split("\n")
  arr.forEach(v => {
    arrItems += '<p>' + v + '</p>'
  })
  return arrItems
}
// 防止多次点击
const throttle = (fn, gapTime) => {
  if (gapTime == null || gapTime == undefined) {
    gapTime = 1500
  }
  let _lastTime = null
  // 返回新的函数
  return function () {
    let _nowTime = +new Date()
    if (_nowTime - _lastTime > gapTime || !_lastTime) {
      fn.apply(this, arguments) //将this和参数传给原函数
      _lastTime = _nowTime
    }
  }
}
// 最多保留两位小数点并且去除后面无效的0
const numFormat = function (num, decimals = 2) {
  return parseFloat(Number(num).toFixed(decimals))
}

// 固定保留一位小数点并且去除后面无效的0
const numFormatOne = function (num, decimals = 1) {
  return Number(num).toFixed(decimals)
}

// 秒转分钟
const getMinute = function (num) {
  let number = Number(num)
  let second = parseInt(number % 60) > 10 ? parseInt(number % 60) : '0' + parseInt(number % 60)
  return parseInt(number / 60) + ':' + second
}

export default {
  getheight,
  isArray,
  isEmpty,
  getStrlen,
  cutString,
  strFormat,
  numFormat,
  numFormatOne,
  throttle,
  getMinute
}
